package com.lishe.lishe.lishe.module.coupon.adapter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.lishe.lishe.R;
import com.lishe.lishe.lishe.MyRecyclerView.RecyclerviewBaseAdapter;
import com.lishe.lishe.lishe.common.AppManager;
import com.lishe.lishe.lishe.module.commonclass.TitleWebActivity;
import com.lishe.lishe.lishe.module.commonclass.WebActivity;
import com.lishe.lishe.lishe.module.coupon.bean.CouponListBean;
import com.lishe.lishe.lishe.module.main.event.TabChangeEvent;

import org.greenrobot.eventbus.EventBus;

import java.util.List;

/**
 * Created by Bin
 */
public class CouponListAdapter extends RecyclerviewBaseAdapter {

    public static final int COUPON_UNUSE = -1;
    public static final int COUPON_USED = -2;
    public static final int COUPON_OVERDUE = -3;

    private Context mContext;
    private int mAdapterType;
    private List<CouponListBean.DataBean.ListBean> mList;

    public CouponListAdapter(Context context, int type, List<CouponListBean.DataBean.ListBean> list) {
        this.mList = list;
        this.mContext = context;
        this.mAdapterType = type;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolderMy(ViewGroup parent, int viewType) {
        View view = null;
        switch (mAdapterType) {
            case COUPON_UNUSE:
                view = LayoutInflater.from(mContext).inflate(R.layout.item_couponlist, null, false);
                break;
            case COUPON_USED:
                view = LayoutInflater.from(mContext).inflate(R.layout.item_couponlist_used, null, false);
                break;
            case COUPON_OVERDUE:
                view = LayoutInflater.from(mContext).inflate(R.layout.item_couponlist_overdue, null, false);
                break;
        }
        ItemViewHolder holder = new ItemViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolderMy(RecyclerView.ViewHolder holder, final int position) {
        ItemViewHolder viewHolder = (ItemViewHolder) holder;
        viewHolder.date.setText(mList.get(position).getCoupon_use_start_date() + "-" + mList.get(position).getCoupon_use_end_date());
        switch (mList.get(position).getCoupon_type()) {
            case "1":
                viewHolder.couponType.setText("平台");
                break;
            case "2":
                viewHolder.couponType.setText("店铺");
                break;
            case "3":
                viewHolder.couponType.setText("企业");
                break;
            case "4":
                viewHolder.couponType.setText("活动");
                break;
        }
        if (mAdapterType == COUPON_UNUSE) {
            viewHolder.use.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //点击去使用按钮，如果是平台券则跳转到首页；如果是活动券则跳转到活动链接，链接取后台配置优惠券时填写的活动链接
                    if ("4".equals(mList.get(position).getCoupon_type())) {
                        /*Intent i = new Intent(mContext, TitleWebActivity.class);
                        i.putExtra(TitleWebActivity.URL, mList.get(position).getCoupon_link());
                        i.putExtra(TitleWebActivity.TITLE, mList.get(position).getCoupon_activity_name());
                        mContext.startActivity(i);*/
                        WebActivity.jump((Activity) mContext, mList.get(position).getCoupon_link(), mList.get(position).getCoupon_activity_name());
                    } else {
                        AppManager.getAppManager().finishActivity();
                        EventBus.getDefault().post(new TabChangeEvent(0));
                    }
                }
            });
        }
        viewHolder.couponName.setText(mList.get(position).getCoupon_type_detail());
        viewHolder.condition.setText(mList.get(position).getCoupon_rule_detail());
        switch (mList.get(position).getCoupon_rule()) {  //优惠券规则
            //1：满减，2：满折，3：满免邮，4：直减
            case "1":
                viewHolder.pricelayout.setVisibility(View.VISIBLE);
                viewHolder.price.setText(mList.get(position).getCoupon_decrease());
                viewHolder.discountlayout.setVisibility(View.GONE);
                viewHolder.freightlayout.setVisibility(View.GONE);
                break;
            case "2":
                viewHolder.pricelayout.setVisibility(View.GONE);
                viewHolder.freightlayout.setVisibility(View.GONE);
                viewHolder.discountlayout.setVisibility(View.VISIBLE);
                viewHolder.discount.setText(mList.get(position).getCoupon_discount());
                break;
            case "3":
                viewHolder.pricelayout.setVisibility(View.GONE);
                viewHolder.discountlayout.setVisibility(View.GONE);
                viewHolder.freightlayout.setVisibility(View.VISIBLE);
                break;
            case "4":
                viewHolder.pricelayout.setVisibility(View.VISIBLE);
                viewHolder.price.setText(mList.get(position).getCoupon_decrease());
                viewHolder.discountlayout.setVisibility(View.GONE);
                viewHolder.freightlayout.setVisibility(View.GONE);
                break;
        }
    }

    @Override
    public int getItemCountMy() {
        return mList == null ? 0 : mList.size();
    }

    @Override
    public int getItemViewTypeMy(int position) {
        return 0;
    }


    class ItemViewHolder extends RecyclerView.ViewHolder {

        TextView price;
        TextView condition;
        TextView couponType;
        TextView couponName;
        TextView date;
        TextView use;
        View pricelayout;
        View discountlayout;
        View freightlayout;
        TextView discount;

        public ItemViewHolder(View v) {
            super(v);
            price = v.findViewById(R.id.price);
            condition = v.findViewById(R.id.condition);
            couponType = v.findViewById(R.id.couponType);
            couponName = v.findViewById(R.id.couponName);
            date = v.findViewById(R.id.date);
            use = v.findViewById(R.id.use);
            pricelayout = v.findViewById(R.id.pricelayout);
            discountlayout = v.findViewById(R.id.discountlayout);
            freightlayout = v.findViewById(R.id.freightlayout);
            discount = v.findViewById(R.id.discount);
        }
    }


}
